package org.sean.framework.bean;


import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.sean.framework.Constants;

/**
 * 分页请求
 */
@Data
@Schema(description = "分页查询")
public class PageQuery {
    public static final String ORDER_ASC = "asc";
    public static final String ORDER_DESC = "desc";

    @Schema(description = "页码")
    private Integer page;

    @Schema(description = "每页条数")
    private Integer size;

    @Schema(description = "排序字段")
    private String sortField;

    @Schema(description = "排序规则")
    private String sortOrder;

    @Schema(description = "上页标记点")
    private String lastPosition;

    public int getLimit() {
        return getSize();
    }

    public int getOffset() {
        return (Math.max((getPage() - 1), 0)) * getSize();
    }

    public Integer getPage() {
        return (page == null || page < Constants.PAGE_MIN_SIZE) ? Constants.PAGE_MIN_SIZE : page;
    }

    public Integer getSize() {
        if (size == null) {
            return Constants.PAGE_DEFAULT_SIZE;
        }
        return size > Constants.PAGE_MAX_SIZE ? Constants.PAGE_MAX_SIZE : size;
    }


}
